﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using DalNHibernat.Implementacao;

namespace DalNHibernat
{
    //TODO: Heng Implementar o Nhibernat = a da DalCliente 
    //ClientRepository cliRep = new ClientRepository(session);
    public class NhibernateTeste : ApplicationBase
    {
        private DalCliente clientRepository;

        public NhibernateTeste()
        {
            clientRepository = new DalCliente(Session);

        }

        public void Update(Modelo.Cliente cliente)
        {
            var ts = Session.BeginTransaction();
            try
            {
                clientRepository.Update(cliente);
                ts.Commit();
            }
            catch (Exception)
            {
                ts.Rollback();
                throw;
            }
        }

        public IList<Modelo.Cliente> ListaClientes()
        {
            return clientRepository.ListaClientes();
        }

        public void Delete(long id)
        {
            var ts = Session.BeginTransaction();

            try
            {
                clientRepository.Delete(clientRepository.RetornaClienteID(id));
                ts.Commit();
            }
            catch (Exception)
            {
                ts.Rollback();
                throw;
            }
        }

        public Modelo.Cliente GetByName(string name)
        {
            return clientRepository.GetByName(name);
        }
    }
}
